package com.mohan.aop.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

@Slf4j
@Aspect
public class LogAspects {

    // 定义一个切入点，我们可以抽出来一个公共的切入点表达式
    @Pointcut("execution(public int com.mohan.aop.service.MathCalculator.*(..))")
    public void pointcut() {
    }

    // @Before: 在方法执行之前切入
    @Before("pointcut()")
    public void logStart(JoinPoint joinPoint) {
        final Object[] args = joinPoint.getArgs();
        System.out.println(joinPoint.getSignature().getName() +
                "除法运行.....@Before，参数列表是：{" + Arrays.asList(args) + "}");
    }

    // @After("point"))
    @After("pointcut()")
    public void logEnd(JoinPoint joinPoint) {

        System.out.println(joinPoint.getSignature().getName() + "除法运行完成.....@After,参数列表是：{}");
    }

    // 在返回之后执行
    @AfterReturning(value = "pointcut()", returning = "result")
    public void logReturn(JoinPoint joinPoint, Object result) {
        System.out.println(joinPoint.getSignature().getName() +
                "除法正常返回......@AfterRetrning，运行结果是：{" + result + "}");
    }

    // 抛异常执行
    @AfterThrowing(value = "pointcut()", throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint, Exception exception) {
        System.out.println(joinPoint.getSignature().getName() +
                "除法出现异常.....异常信息：{" + exception + "}");
    }
}
